//
// User Schema
//
var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');

var schema = mongoose.Schema({
   // general
   firstname      : String,
   lastname       : String,
   //club           : {type: mongoose.Schema.Types.ObjectId, ref: 'club'}, // own club
   //joinedclubs    : [{type: mongoose.Schema.Types.ObjectId, ref: 'club'}], // joined club
   status_bit     : Number,   // 0: delete 1: active 2: inactive
   role           : String,   // lower case: admin, canread, canwrite
   rememberme     : String,   // _rme cookie hashed token
   login_attempts : Number,
   locked_until   : Date,
   userdetail     : {type: mongoose.Schema.Types.ObjectId, ref: 'userdetail'},

   // credential
   local          : {
      email       : String,
      password    : String
   },
   facebook       : {
      id          : String,
      token       : String,
      email       : String,
      name        : String
   },
   twitter        : {
      id          : String,
      token       : String,
      displayName : String,
      username    : String
   },
   google         : {
      id          : String,
      token       : String,
      email       : String,
      name        : String
   }
});


// methods
schema.methods.generateHash = function(password) {
   return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
schema.methods.validPassword = function(password) {
   return bcrypt.compareSync(password, this.local.password);
};

module.exports  = schema;
